package genome;


import static org.junit.Assert.*;

import java.util.ArrayList;

import org.junit.*;

import tool.RandomGenerator;
import tool.RepeatRandom;
import tool.Utility;

public class CGPTest {

	private CGP g1;
	private CGP g2;
	private double[] default_input = {0.0,1.0};
	private int[] sample_genome1 = {0,0,0,1,1,1};
	private int[] sample_genome2 = {0,0,1,0,2,1};
	private int default_size = 6;
	
	private RepeatRandom r = RandomGenerator.R;
	
	@Before
	public void setUp() throws Exception {
		g1 = new CGP(default_input,default_size);
		g2 = new CGP(default_input,default_size);
		g1.genome=sample_genome1;
		g2.genome=sample_genome2;
		

	}
	
	@Test
	public void toMathStringTest(){
		System.out.println(g1.toMathString());
		System.out.println(g2.toMathString());
		assertTrue(g1.toMathString().equals("[[1]-[1]]"));
		assertTrue(g2.toMathString().equals("[[0]-[[0]-[0]]]"));
	}
	
	@Test
	public void evalTest(){
		System.out.println(g1.eval());
		System.out.println(g2.eval());
	}
	
	@Test
	public void sampleSpaceTest(){
		ArrayList<Genotype> space = g1.generateSpace(10);
		assertTrue(space.size()==10);
	}
	
	@Test
	public void differenceTest(){
		double diff1 = g1.diff(g2);
		double diff2 = g2.diff(g1);
		assertTrue(diff1==diff2);
	}
	
	@Test
	public void distanceTest(){
		double d1 = g1.distanceTo(g2);
		double d2 = g2.distanceTo(g1);
		assertTrue(d1==d2);
		
		//Check against manually calculated values for 0.1 mutation rate and the default genomes
		double pSame = 0.95*0.95*(0.9+0.1/3)*(0.9+0.1/g1.NUM_FUNCTIONS);
		double pDiff = (0.1/g1.NUM_FUNCTIONS)*(0.1/3);
		
		double expectedProbability = Utility.roundToSignificantFigures(pSame*pDiff, 5);
		
		assertTrue(d1==expectedProbability);
		
	}
	
	@Test
	public void stoMutateTest(){
		CGP g3 = (CGP)g1.stoMutate();
		System.out.println(g1);
		System.out.println(g3);
		System.out.println("----");
		int allele3equalTo1 = 0;
		int allele3equalTo2 = 0;
		int allele3equalTo3 = 0;
		int allele4equalTo2 = 0;
		int allele6equalTo2 = 0;
		for (int i = 0; i < 1000; i++) {
			g3 = (CGP)g1.stoMutate();
			//System.out.println(g3);
			if(g3.genome[2]==1){allele3equalTo1 = allele3equalTo1+1;}
			if(g3.genome[2]==2){allele3equalTo2 = allele3equalTo2+1;}
			if(g3.genome[2]==3){allele3equalTo3 = allele3equalTo3+1;}
			if(g3.genome[3]==2){allele4equalTo2 = allele4equalTo2+1;}
			if(g3.genome[5]==2){allele6equalTo2 = allele6equalTo2+1;}
		}
		System.out.println(allele3equalTo1);
		System.out.println(allele3equalTo2);
		System.out.println(allele3equalTo3);
		System.out.println(allele4equalTo2);
		System.out.println(allele6equalTo2);
	}
	
}
